/*
 * Copyright (C) 2008 by Xume.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.xume.critann.annotations.criterion;

import java.util.ArrayList;
import java.util.List;

import org.xume.critann.CriteriaBuilder;
import org.xume.critann.callback.MethodAnnotationCallback;
import org.xume.critann.value.ParameterValue;

/**
 * $Id: ConjunctionCallback.java 26 2008-10-16 12:45:35Z johan.siebens $
 * 
 * @author Johan Siebens
 */
public class ConjunctionCallback implements MethodAnnotationCallback<Conjunction> {

	private List<CriterionCallbackImpl> callbacks;

	public <C, P> void doWithCriteriaBuilder(CriteriaBuilder<C, P> builder, Object criteriaBean) {
		List<C> criterions = new ArrayList<C>();
		for (CriterionCallbackImpl callback : callbacks) {
			criterions.add(callback.createCriterion(builder, criteriaBean));
		}
		builder.addCriterion(builder.conjunction(criterions));
	}

	public void initialize(Class<?> criteriaBeanClass, Conjunction conjunction, ParameterValue value) {
		this.callbacks = new ArrayList<CriterionCallbackImpl>();
		for (Criterion criterion : conjunction.criterions()) {
			CriterionCallbackImpl callback = new CriterionCallbackImpl();
			callback.initialize(criteriaBeanClass, criterion, value);
			this.callbacks.add(callback);
		}
	}

}
